﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Globalization;
using System.Resources;
using System.IO;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Controls;
using System.Threading;

namespace SqlMiM.Common.Framework
{
    public sealed class FileResourceManager
    {
        // Fields
        private string componentName;
        private ResourceManager resourceManager;

        // Methods
        public FileResourceManager(Assembly resourceAssembly)
        {
            string name = resourceAssembly.ManifestModule.Name;
            if (name.EndsWith(".DLL", true, CultureInfo.InvariantCulture) || name.EndsWith(".EXE", true, CultureInfo.InvariantCulture))
            {
                name = name.Substring(0, name.Length - 4);
            }
            name = name + ".g";
            this.resourceManager = new ResourceManager(name, resourceAssembly);
            this.componentName = resourceAssembly.ToString();
        }

        public byte[] GetByteArray(string name)
        {
            name = name.ToLower(CultureInfo.InvariantCulture);
            name = name.Replace(@"\", "/");
            Stream stream = (Stream)this.resourceManager.GetObject(name, Thread.CurrentThread.CurrentUICulture);
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        public Cursor GetCursor(string name)
        {
            name = name.ToLower(CultureInfo.InvariantCulture);
            name = name.Replace(@"\", "/");
            Stream cursorStream = (Stream)this.resourceManager.GetObject(name, Thread.CurrentThread.CurrentUICulture);
            Cursor cursor = new Cursor(cursorStream);
            return cursor;
        }

        public DataTemplate GetDataTemplate(string name)
        {
            ResourceDictionary dictionary = (ResourceDictionary)this.LoadObject(name);
            DataTemplate template = (DataTemplate)dictionary["root"];
            return template;
        }

        public DrawingImage GetDrawingImage(string name)
        {
            DrawingImage image = (DrawingImage)this.LoadObject(name);
            return image;
        }

        public FrameworkElement GetElement(string name)
        {
            FrameworkElement element = (FrameworkElement)this.LoadObject(name);
            return element;
        }

        public FontFamily GetFontFamily(string name)
        {
            return (FontFamily)this.LoadObject(name);
        }

        public ImageSource GetImageSource(string name)
        {
            ImageSource imageSourceCore = this.GetImageSourceCore(name);
            return imageSourceCore;
        }

        private ImageSource GetImageSourceCore(string name)
        {
            name = name.ToLower(CultureInfo.InvariantCulture);
            name = name.Replace(@"\", "/");
            Stream stream = (Stream)this.resourceManager.GetObject(name, Thread.CurrentThread.CurrentUICulture);
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = stream;
            image.EndInit();
            image.Freeze();
            return image;
        }

        public Model3DGroup GetModel3DGroup(string name)
        {
            Model3DGroup group = (Model3DGroup)this.LoadObject(name);
            return group;
        }

        public ResourceDictionary GetResourceDictionary(string name)
        {
            object obj2 = this.LoadObject(name);
            ResourceDictionary dictionary = obj2 as ResourceDictionary;
            if (dictionary != null)
            {
                return dictionary;
            }

            FrameworkElement element = obj2 as FrameworkElement;
            if (element != null)
            {
                return element.Resources;
            }
            return null;
        }

        public Style GetStyle(string name)
        {
            Control control = (Control)this.LoadObject(name);
            Style tag = control.Tag as Style;
            return tag;
        }

        private object LoadObject(string name)
        {
            name = name.ToLower(CultureInfo.InvariantCulture);
            name = name.Replace(@"\", "/");
            Uri resourceLocator = new Uri(this.componentName + ";component/Resources/" + Path.ChangeExtension(name, ".xaml"), UriKind.RelativeOrAbsolute);
            return Application.LoadComponent(resourceLocator);
        }
    }


}
